package com.qf.utils.filter;

import com.qf.bean.Employee;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * IntelliJ IDEA -> person
 * 2019/11/22 19:05
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
public class ExceUtils {
    //    先创建一个Exce文件
    static  HSSFWorkbook  workbook;
    //创建文件中的sheet表 ， 参数为sheet的名字
    static  HSSFSheet sheet;
    public static void createFirst(String sheetName){
        workbook  = new HSSFWorkbook();
        sheet = workbook.createSheet(sheetName);
            HSSFRow row = sheet.createRow(0);//创建第一行
            String[] heads = {"账号","姓名","角色" ,"所属部门"};
            for (int i = 0; i < heads.length; i++) {
                HSSFCell cell = row.createCell(i);//创建列
                cell.setCellValue(heads[i]);
            }
        }
        //创建除了第一行以外的其他行，参数表示的是被导出的是数据集合
        public  static void  createOthers(List<Employee > list){
            for (int i = 0; i < list.size(); i++) {
                HSSFRow row = sheet.createRow(i + 1);
                Employee employee = list.get(i);
                HSSFCell cell = row.createCell(0);
                cell.setCellValue(employee.getLoginname());
                HSSFCell cell1 = row.createCell(1);
                cell1.setCellValue(employee.getUsername());
                HSSFCell cell2 = row.createCell(2);
                cell2.setCellValue(employee.getRole().getRname());
                HSSFCell cell3 = row.createCell(3);
                cell3.setCellValue(employee.getDept().getDeptname());
            }
        }

        public  static void  export(OutputStream outputStream) throws IOException {
            sheet.setGridsPrinted(true);
            workbook.write(outputStream);
        }
}
